"""
Case Type   : dolphin
Case Name   : 在C类型、PG类型数据库下创建插件，合理报错
Create At   : 2022.04.06
Owner       : z@wan005
Description :
    1、创建c库和PG库
    2、c库下创建extension
    3、PG库下创建extension
    4、清理环境
Expect      :
    1、成功
    2、报错
    3、报错
    4、清理环境完成
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class PluginTest03(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primysh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_plugin0003'
        self.db_name_01 = 'db_plugin0003_01'
        self.assert_res = ('ERROR:  extension "dolphin" is only supported in '
                           'B type database')

    def test_dolphin(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '----step1:创建c库和PG库;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} DBCOMPATIBILITY ='C';" \
                  f"drop database if exists {self.db_name_01};" \
                  f"create database {self.db_name_01} DBCOMPATIBILITY ='PG';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primysh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEquals(2,
                          sql_res.count(self.constant.CREATE_DATABASE_SUCCESS),
                          '执行失败' + text)

        text = '---step2:c库下创建extension;expect:报错---'
        self.log.info(text)
        sql_cmd = 'create extension dolphin;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primysh.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn(self.assert_res, sql_res, '执行失败' + text)

        text = '--step3:兼容PG库下创建插件;expect:报错--'
        self.log.info(text)
        sql_cmd = 'create extension dolphin;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primysh.execut_db_sql(sql_cmd,
                                                dbname=self.db_name_01)
        self.log.info(sql_res)
        self.assertIn(self.assert_res, sql_res, '执行失败' + text)

    def tearDown(self):
        text = '--step4:清理环境;expect:清理环境完成--'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"drop database if exists {self.db_name_01};"
        self.log.info(sql_cmd)
        sql_res = self.sh_primysh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEquals(2,
                          sql_res.count(self.constant.DROP_DATABASE_SUCCESS),
                          '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
